﻿using System;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;

namespace HoangPhap.Utilities
{
    public class Mail
    {
        public string SmtpServer { get; set; }
        public string EmailUsername { get; set; }
        public string EmailPassword { get; set; }
        public string EmailAddress { get; set; }
        public string From { get; set; }
        public string To { get; set; }
        public string Cc { get; set; }
        public string Bcc { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }

        /// <summary>
        /// Contructor
        /// </summary>
        public Mail()
        {
            SmtpServer = String.Empty;
            EmailUsername = String.Empty;
            EmailPassword = String.Empty;
            EmailAddress = String.Empty;
            From = String.Empty;
            To = String.Empty;
            Cc = String.Empty;
            Bcc = String.Empty;
            Subject = String.Empty;
            Body = String.Empty;
        }

        /// <summary>
        /// Contructor để khai báo Smtp Server
        /// </summary>
        /// <param name="smtpServer">Server SMTP</param>
        /// <param name="emailAddress">Email dùng để gửi</param>
        public Mail(string smtpServer, string emailAddress)
        {
            SmtpServer = smtpServer;
            EmailAddress = emailAddress;
        }

        /// <summary>
        /// Contructor khai báo đầy đủ (username, password)
        /// </summary>
        /// <param name="smtpServer">erver SMTP</param>
        /// <param name="emailUsername">User để truy cập vào Smtp Server</param>
        /// <param name="emailPassword">Password của User</param>
        /// <param name="emailAddress">Email dùng để gửi</param>
        public Mail(string smtpServer, string emailUsername, string emailPassword, string emailAddress)
        {
            SmtpServer = smtpServer;
            EmailUsername = emailUsername;
            EmailPassword = emailPassword;
            EmailAddress = emailAddress;
        }

        /// <summary>
        /// Hàm gửi mail 1
        /// </summary>
        /// <param name="from">Gửi từ Email</param>
        /// <param name="to">Gửi đến Email</param>
        /// <param name="subject">Tiêu đề</param>
        /// <param name="body">Nội dung</param>
        /// <returns>True|False</returns>
        public bool Send(string from, string to, string subject, string body)
        {
            From = from;
            To = to;
            Subject = subject;
            Body = body;
            return Send();
        }

        /// <summary>
        /// Hàm gửi mail 2
        /// </summary>
        /// <param name="to">Gửi đến Email</param>
        /// <param name="subject">Tiêu đề</param>
        /// <param name="body">Nội dung</param>
        /// <param name="useSsl">Có sử dụng SSL khi gửi không</param>
        /// <param name="cC">Email CC</param>
        /// <param name="bCc">Email BCC</param>
        /// <param name="vTypeId"></param>
        /// <returns></returns>
        public object Send(string to, string subject, string body, bool useSsl = false, string cC = "",
            string bCc = "", int vTypeId = 0)
        {
            From = EmailAddress;
            To = to;
            Subject = subject;
            Body = body;
            Cc = cC;
            Bcc = bCc;
            return Send(useSsl);
        }

        public bool Send(bool useSsl = false)
        {
            var msg = new MailMessage();
            var smtp = new SmtpClient();
            const string strPatten = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
            try
            {
                msg.SubjectEncoding = Encoding.UTF8;
                msg.BodyEncoding = Encoding.UTF8;
                msg.IsBodyHtml = true;
                switch (From.Trim())
                {
                    case "webmaster@meotom.net":
                        msg.From = new MailAddress(From, "WebMaster[at]meotom.net");
                        break;
                    case "service@vnexpress.net":
                        msg.From = new MailAddress(From, "VnExpress.Net");
                        break;
                    default:
                        msg.From = new MailAddress(From);
                        break;
                }
                msg.Subject = Subject;
                msg.Body = Body;

                if (!(To.Length < 1))
                {
                    To = To.Replace(";", ",").Replace(">", "").Replace("<", "");
                    Cc = Cc.Replace(";", ",").Replace(">", "").Replace("<", "");
                    Bcc = Bcc.Replace(";", ",").Replace(">", "").Replace("<", "");

                    msg.To.Add(To);

                    if (!(Cc.Length < 1))
                    {
                        foreach (var strMailCc in
                            Regex.Split(Cc, ",").Where(strMailCc => strMailCc.Length > 1 & Regex.IsMatch(strMailCc, strPatten)))
                        {
                            msg.CC.Add(strMailCc);
                        }
                    }
                    if (!(Bcc.Length < 1))
                    {
                        foreach (var strMailBcc in
                            Regex.Split(Bcc, ",").Where(strMailBcc => strMailBcc.Length > 1 & Regex.IsMatch(strMailBcc, strPatten)))
                        {
                            msg.Bcc.Add(strMailBcc);
                        }
                    }

                    smtp.Host = SmtpServer;
                    smtp.Credentials = new NetworkCredential(EmailUsername, EmailPassword);
                    if (useSsl)
                    {
                        smtp.Port = 587;
                        smtp.EnableSsl = true;
                    }
                    smtp.Send(msg);
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }

}